package com.jair.manager.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.jair.common.bean.EasyUIResult;
import com.jair.manage.pojo.ItemParam;
import com.jair.manage.service.ItemParamService;

/**
 * @Description 
 * @FullName com.jair.manager.controller.ItemParamController 
 * @Author Jair_Huang
 * @Create 2017年5月26日 下午4:49:52   
 */
@Controller
@RequestMapping("item/param")
public class ItemParamController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ItemParamController.class);

    @Autowired
    private ItemParamService itemParamService;

    /**
     * 根据类目id查找模板
     * @param itemCatId
     * @return ResponseEntity<ItemParam>
     * @Create By Jair_Huang on 2017年5月26日 下午4:59:01
     */
    @RequestMapping(value = "{itemCatId}", method = RequestMethod.GET)
    private ResponseEntity<ItemParam> queryByItemCatId(@PathVariable("itemCatId") Long itemCatId) {
        try {
            ItemParam param = new ItemParam();
            param.setItemCatId(itemCatId);
            ItemParam itemParam = this.itemParamService.queryOne(param);
            if (null == itemParam) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(itemParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 新增模板
     *
     * @param itemCatId
     * @return
     */
    @RequestMapping(value = "{itemCatId}", method = RequestMethod.POST)
    public ResponseEntity<Void> saveItemParam(@PathVariable("itemCatId") Long itemCatId,
            @RequestParam("paramData") String paramData) {
        try {
            ItemParam param = new ItemParam();
            param.setItemCatId(itemCatId);
            param.setParamData(paramData);
            this.itemParamService.save(param);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 查询模板列表
     * 
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<EasyUIResult> queryList(@RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows) {
        try {
            PageInfo<ItemParam> pageInfo = this.itemParamService.queryPageListByWhere(null, page, rows);
            return ResponseEntity.ok(new EasyUIResult(pageInfo.getTotal(), pageInfo.getList()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 删除商品参数
     * @param page
     * @param rows
     * @return ResponseEntity<EasyUIResult>
     * @Create By Jair_Huang on 2017年5月26日 下午10:37:00
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseEntity<Void> deleteItemParamByIds(@RequestParam("ids") List<Object> ids) {
        try {
            this.itemParamService.deleteByIds(ids, "id", ItemParam.class);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("删除！desc = {}", ids);
            }
            return ResponseEntity.status(HttpStatus.OK).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}
